"""

这个文件是项目的入口文件
"""
import logging

from info import create_app, db, models
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from info.models import User

app = create_app("develop")  # 需求:创建app的时候可以传参数决定要加载哪些配置项(DevelopmentConfig,或者ProductionConfig)
# 传develop的时候,加载DevelopmentConfig类里面的配置
# 传product的时候,加载ProductionConfig类里面的配置
manager = Manager(app)
Migrate(app, db)
manager.add_command("db", MigrateCommand)


@manager.option("-n", dest="name")
@manager.option("-p", dest="password")
def createsuperuser(name, password):

    if not all([name ,password]):
        print("参数不全")
        return

    user = User()
    user.nick_name = name
    user.mobile = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
        print("创建成功")
        return
    except Exception as e:
        print("创建失败： %s" % e)
        db.session.rollback()


if __name__ == "__main__":
    # print(app.url_map)
    manager.run()
